<?php

class CashbackAction extends CommonAction
{

    public function daily()
    {
        set_time_limit(0);
        $var = $_GET['var'];
        if ($var == "sdgsadgeosgnedognospdg574g8sd4g2rfg4d8g7d8zghspgeorfr4657*9+") {
            $Count = D("CashbackCount");
            $Count->cashbackByDay();
            $Count->adjustByDay();
            
            
			
			
            import("@.ORG.Affiliate.Rangelevelbackmoney");
            $class = 'Rangelevelbackmoney';
            $Threelevel = new $class(0, 296);
            $Threelevel->moneyback();
        }
    }

    public function backmoney()
    {
        ini_set('memory_limit', '2048M');
        
        set_time_limit(0);
        
        $this->saledb = M('Sale');
        $product_sql = M('Cashback_config')->where(array(
            'site_id' => 156,
            'total' => 0
        ))
            ->Field('product_id')
            ->buildSql();
        $list = $this->saledb->where("product_id in(" . $product_sql . ") and pay_status = 1")
            ->order('id asc')
            ->group('member_id')
            ->Field('member_id, (sum(total)) as total,product_id')
            ->select();
        // dump($list);
        $db = M('Cashback_count');
        foreach ($list as $k => $v) {
            dump($v['total']);
            
            $data['已返'] = $db->where(array(
                'member_id' => $v['member_id']
            ))->sum('total');
            // 所有下级返现总额
            
            $data['需返'] = (float) $v['total'] * 40 * 0.021;
            
            // dump($data['需返']);
            
            $data['member_id'] = $v['member_id'];
            $sids = M('Member')->where(array(
                'pid' => $v['member_id']
            ))
                ->Field('id')
                ->select();
            
            foreach ($sids as $sidsk => $sidsv) {
                
                // 他的所有下家返现消费
                $sale = $this->saledb->where("product_id in(" . $product_sql . ") and pay_status = 1 and member_id in(" . $sidsv['id'] . ")")
                    ->Field('id,product_id,total,order_id')
                    ->order('id asc')
                    ->select();
                if ($sale) {
                    // dump($sale);
                }
                $c = array();
                $c[] = $this->saledb->where("product_id in(" . $product_sql . ") and pay_status = 1 and member_id in(" . $sidsv['id'] . ")")
                    ->order('id asc')
                    ->sum('total');
                $c[] = $v['total'];
                if ((float) min($c) <= 0) {
                    continue;
                }
                // dump(min($c) * 0.011 * 40);
                $data['需返'] += min($c) * 0.011 * 40;
            }
            
            if (round((float) ($data['需返'] - $data['已返']), 2) > 0) {
                $insertdata['site_id'] = 156;
                $insertdata['member_id'] = $v['member_id'];
                $insertdata['total'] = $data['需返'] - $data['已返'];
                $insertdata['rest'] = $data['需返'] - $data['已返'];
                $insertdata['last_adjust'] = time();
                $insertdata['product_id'] = $v['product_id'];
                $insertdata['rate'] = 250;
                $insertdata['last_money'] = $data['需返'] - $data['已返'];
                M('Cashback_count')->add($insertdata);
                dump($insertdata);
            }
        }
    }
} 
